﻿/*  ----------------------------------------------------------------------------
 *  Author:
 *  Chandra Shekhar Tripathi
 *  ----------------------------------------------------------------------------
 *  Product Name:
 *  DbDocumenter
 *  ----------------------------------------------------------------------------
 *  Year:
 *  2009
 *  ----------------------------------------------------------------------------
 *  File:
 *  MainWindow.xaml.cs
 *  ----------------------------------------------------------------------------
 */
namespace DBDocumenter
{
    using System;
    using System.ComponentModel;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media.Animation;

    using DBDocumenter.BLL.Services;
    using DBDocumenter.Exception;
    using DBDocumenter.Helper;
    using DBDocumenter.Resources;
    using DBDocumenter.RoutedEvents;

    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        #region Fields

        Setting applicationSetting = new Setting();
        private int currentStep;
        private WindowState m_storedWindowState = WindowState.Normal;
        private System.Windows.Forms.NotifyIcon notifyIcon;

        #endregion Fields

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="MainWindow"/> class.
        /// </summary>
        public MainWindow()
        {
            InitializeComponent();
            currentStep = 1;
            notifyIcon = new System.Windows.Forms.NotifyIcon();
            notifyIcon.BalloonTipText = PLResources.ApplicationMinimizeMessage;
            notifyIcon.BalloonTipTitle = PLResources.ApplicationName;
            notifyIcon.Text = PLResources.ApplicationName;
            notifyIcon.Icon = new System.Drawing.Icon(PLResources.Application, new System.Drawing.Size());
            notifyIcon.Click += new EventHandler(notifyIcon_Click);
        }

        #endregion Constructors

        #region Events

        /// <summary>
        /// Occurs when [show pop up event handler].
        /// </summary>
        public event RoutedEventHandler ShowPopUpEventHandler
        {
            add
            {
                this.AddHandler(DBDocumenterEvents.ShowPopUpRoutedEvent, value);
            }
            remove
            {
                this.RemoveHandler(DBDocumenterEvents.ShowPopUpRoutedEvent, value);
            }
        }

        #endregion Events

        #region Methods

        /// <summary>
        /// Checks the tray icon.
        /// </summary>
        private void CheckTrayIcon()
        {
            ShowTrayIcon(!IsVisible);
        }

        /// <summary>
        /// Handles the Completed event of the HideWindowStoryboard control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void HideWindowStoryboard_Completed(object sender, EventArgs e)
        {
            SetWizard();
        }

        /// <summary>
        /// Called when [theme change].
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void OnThemeChange(object sender, RoutedEventArgs e)
        {
            try
            {
                Button b = sender as Button;
                applicationSetting.CurrentTheme = b.Name.ToString() + ".xaml";
                UtilityManager.Instance.SetSettings(applicationSetting);
                this.SetCurrentTheme();
            }
            catch (System.Exception ex)
            {
                Logger.WriteLog(LogType.PresentationLayer, LogLevel.Error, ex.Message);
            }
        }

        /// <summary>
        /// Sets the current theme.
        /// </summary>
        void SetCurrentTheme()
        {
            try
            {
                ResourceDictionary r = new ResourceDictionary();
                r.Source = new Uri(applicationSetting.CurrentTheme, UriKind.Relative);
                Application.Current.Resources.MergedDictionaries.Clear();
                Application.Current.Resources.MergedDictionaries.Add(r);
            }
            catch(System.Exception ex) 
            {
                Logger.WriteLog(LogType.PresentationLayer, LogLevel.Error, ex.Message);
            }
        }

        /// <summary>
        /// Sets the wizard.
        /// </summary>
        private void SetWizard()
        {
            btnNext.Visibility = Visibility.Visible;
            btnPrevious.Visibility = Visibility.Visible;

            if (currentStep.Equals(1))
            {
                wzSteps.SetWizardStep(WizardSteps.Welcome);
                dbInfo.SetUserControl(WizardSteps.Welcome);
                btnPrevious.Visibility = Visibility.Hidden;
            }
            else if (currentStep.Equals(2))
            {
                wzSteps.SetWizardStep(WizardSteps.DatabaseInformation);
                dbInfo.SetUserControl(WizardSteps.DatabaseInformation);
            }
            else if (currentStep.Equals(3))
            {
                wzSteps.SetWizardStep(WizardSteps.DatabaseCompont);
                dbInfo.SetUserControl(WizardSteps.DatabaseCompont);
            }
            else if (currentStep.Equals(4))
            {
                wzSteps.SetWizardStep(WizardSteps.DatabaseSummary);
                dbInfo.SetUserControl(WizardSteps.DatabaseSummary);
                btnNext.Visibility = Visibility.Hidden;
            }

            Storyboard s = (Storyboard)this.Resources["showWindow"];
            s.Begin(this);
        }

        /// <summary>
        /// Shows the pop up.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        void ShowPopUp(object sender, RoutedEventArgs e)
        {
            ShowPopUpEventArgs args = (ShowPopUpEventArgs)e;
            ucPopup.Visibility = args.Value;
        }

        /// <summary>
        /// Shows the tray icon.
        /// </summary>
        /// <param name="show">if set to <c>true</c> [show].</param>
        void ShowTrayIcon(bool show)
        {
            if (notifyIcon != null)
                notifyIcon.Visible = show;
        }

        /// <summary>
        /// Called when [close].
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="args">The <see cref="System.ComponentModel.CancelEventArgs"/> instance containing the event data.</param>
        void Window_Closing(object sender, CancelEventArgs args)
        {
            notifyIcon.Dispose();
            notifyIcon = null;
        }

        /// <summary>
        /// Called when [is visible changed].
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="args">The <see cref="System.Windows.DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
        void Window_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs args)
        {
            CheckTrayIcon();
        }

        /// <summary>
        /// Handles the Loaded event of the Window control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                SetWizard();
                this.ShowPopUpEventHandler += new RoutedEventHandler(ShowPopUp);
                dbInfo.SetStatus += new DBDocumenter.UserControls.UCDatabaseInformation.OnSetStatusEventDelegate(dbInfo_SetStatus);
                UtilityManager.Instance.LoadSettings("Settings.xml");
                applicationSetting = UtilityManager.Instance.GetSetting();
                this.SetCurrentTheme();

            }
            catch (System.Exception ex)
            {
                Logger.WriteLog(LogType.PresentationLayer, LogLevel.Error, ex.Message);
            }
        }

        /// <summary>
        /// Called when [state changed].
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="args">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        void Window_StateChanged(object sender, EventArgs args)
        {
            if (WindowState == WindowState.Minimized)
            {
                Hide();
                if (notifyIcon != null)
                    notifyIcon.ShowBalloonTip(2000);
            }
            else
                m_storedWindowState = WindowState;
        }

        /// <summary>
        /// Handles the Unloaded event of the Window control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void Window_Unloaded(object sender, RoutedEventArgs e)
        {
            this.ShowPopUpEventHandler -= new RoutedEventHandler(ShowPopUp);
            dbInfo.SetStatus -= new DBDocumenter.UserControls.UCDatabaseInformation.OnSetStatusEventDelegate(dbInfo_SetStatus);
        }

        /// <summary>
        /// Handles the Click event of the btnAbout control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void btnAbout_Click(object sender, RoutedEventArgs e)
        {
            About aboutScreen = new About();
            aboutScreen.ShowDialog();
        }

        /// <summary>
        /// Handles the Click event of the btnExit control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void btnExit_Click(object sender, RoutedEventArgs e)
        {
            //UtilityManager.ClearHTMLFolder("XML");
            UtilityManager.ClearFolder(Folders.HTML);
            this.Close();
        }

        /// <summary>
        /// Handles the Click event of the btnNext control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void btnNext_Click(object sender, RoutedEventArgs e)
        {
            Storyboard s = (Storyboard)this.Resources["hideWindow"];
            s.Begin(this);
            currentStep++;
        }

        /// <summary>
        /// Handles the Click event of the btnPrevious control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void btnPrevious_Click(object sender, RoutedEventArgs e)
        {
            Storyboard s = (Storyboard)this.Resources["hideWindow"];
            s.Begin(this);
            currentStep--;
        }

        /// <summary>
        /// Dbs the info_ set status.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="value">The value.</param>
        private void dbInfo_SetStatus(string message, int value)
        {
            try
            {
                this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal,
                    (System.Threading.ThreadStart)delegate()
                {
                    if (value == 0)
                    {
                        ucPopup.SetStatus(string.Empty, value);
                    }
                    ucPopup.SetStatus(message, value);
                }
                    );
            }
            catch (System.Exception ex)
            {
                Logger.WriteLog(LogType.PresentationLayer, LogLevel.Error, ex.Message);
            }
        }

        /// <summary>
        /// Handles the Click event of the m_notifyIcon control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void notifyIcon_Click(object sender, EventArgs e)
        {
            this.Show();
            WindowState = m_storedWindowState;
        }

        #endregion Methods
    }
}